package com.sxt.sys.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.sys.domain.Topic;
import com.sxt.sys.service.TopicService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.vo.TopicVo;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2019-04-29
 */
@RestController
@RequestMapping("/sys/topic")
public class TopicController {

	@Autowired
	private TopicService topicService;
	
	/**
	 * 查询所有帖子
	 */
	@RequestMapping("loadAllTopic")
	public DataGridView loadAllTopic(TopicVo topicVo) {
		IPage<Topic> page = new Page<>(topicVo.getPage(), topicVo.getLimit());
		this.topicService.page(page, null);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	/**
	 * 我要发帖
	 */
	@RequestMapping("addTopic")
	public Map<String, String> addTopic(TopicVo topicVo){
		topicVo.setClickamount(0);
		topicVo.setCreatedate(new Date());
		boolean flag = this.topicService.save(topicVo);
		Map<String, String> map = new HashMap<>();
		if (flag) {
			map.put("msg", "发帖成功");
		} else {
			map.put("msg", "发帖失败");
		}
		return map;
	}
}

